package Mash.UI;

import Mash.MainGame;

import javax.swing.*;
import javax.imageio.ImageIO;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;

import sun.java2d.pipe.BufferedBufImgOps;/*Copyright (c)  Sergey Schekochikhin (Genocidicbunny)
 * created on Jul 30, 2009 4:07:59 PM
 */

public class OptionsMenu extends JPanel implements KeyListener, Runnable {
	private MainGame myMain;
	private Options CURRENTOPTION = Options.XRES, ABOVECURRENT = Options.GOBACK, BELOWCURRENT = Options.YRES;
	private int gameX, gameY;
	private int xsize, ysize;
	private Thread optionsAnimate;
	private boolean running;
	private BufferedImage xsizepic, ysizepic, gobackpic, background;
	private BufferedImage aboveButton, currentButton, belowButton;
	private boolean upBounce = true;
	private int i = 0;

	public void keyTyped( KeyEvent e ) {}
	public void keyPressed( KeyEvent e ) {
		int kcode = e.getKeyCode();
		if(kcode == KeyEvent.VK_ESCAPE)
			System.exit(11);
		else if(kcode == KeyEvent.VK_ENTER){
			if(CURRENTOPTION == Options.GOBACK){
				myMain.showMainMenu();
				resetOptionPlaces();
			}
		}
		else if(kcode == KeyEvent.VK_DOWN){
			rotateOptions(false);
		}
		else if(kcode == KeyEvent.VK_UP){
			rotateOptions(true);
		}
	}
    public void keyReleased( KeyEvent e ) {}
	public OptionsMenu(MainGame m, Dimension d){
		super();
		myMain = m;
		xsize = (int)d.getWidth();
		ysize = (int)d.getHeight();
		setPreferredSize(d);
		addKeyListener(this);
		gameX = 700; gameY = 700;
		startMenu();
		try{
			xsizepic = ImageIO.read(Thread.currentThread().getContextClassLoader().getResource("\\Mash\\UI\\UIImages\\xsizepic.png"));
			ysizepic = ImageIO.read(Thread.currentThread().getContextClassLoader().getResource("\\Mash\\UI\\UIImages\\ysizepic.png"));
			gobackpic = ImageIO.read(Thread.currentThread().getContextClassLoader().getResource("\\Mash\\UI\\UIImages\\gobackpic.png"));
			background = ImageIO.read(Thread.currentThread().getContextClassLoader().getResource("\\Mash\\UI\\UIImages\\background.jpg"));
		}catch( IOException e){e.printStackTrace();}
		aboveButton = gobackpic;
		belowButton = ysizepic;
		currentButton = xsizepic;


	}
	public void paintComponent(Graphics g){
		Graphics2D g2d = (Graphics2D)g;
		g2d.drawImage(background, 0,0, xsize, ysize, null);
		//drawOptions(g2d);
		drawSelected(g2d);

	}
	private void drawOptions(Graphics2D g2d){
		g2d.drawImage(xsizepic, xsize/2-150, 0, 300, 80, null);
		g2d.drawImage(ysizepic, xsize/2-150, 80, 300, 80, null);
		g2d.drawImage(gobackpic, xsize/2-150, 400, 300, 80, null);

	}
	//TODO:fill in with a popup to change options. meanwhile, the default will be 700x700
	private void drawXPopup(Graphics2D g2d){

	}
	private void drawYPopup(Graphics2D g2d){

	}
	public void rotateOptions(boolean up){
		if(up)
			CURRENTOPTION = Options.getPrevious(CURRENTOPTION);
		else
			CURRENTOPTION = Options.getNext(CURRENTOPTION);
		ABOVECURRENT = Options.getPrevious(CURRENTOPTION);
		BELOWCURRENT = Options.getNext(CURRENTOPTION);
		setImages();
	}
	private void setImages(){
		aboveButton = selectButtonImage(ABOVECURRENT);
		belowButton = selectButtonImage(BELOWCURRENT);
		currentButton = selectButtonImage(CURRENTOPTION);
	}
	private void startMenu() {
		if(optionsAnimate == null){
			optionsAnimate = new Thread(this);
			optionsAnimate.start();
			running = true;
		}
	}
    public void run(){
	   while(running){
		   repaint();
		   try{Thread.sleep(100);}catch(InterruptedException ex){System.out.println("Oh no. I told them this would happen. Gordon, get out of there!");}
	   }
	}
	private void drawSelected(Graphics2D g2d){
		//Makes the menu button bounce up and down slightly to give it an emphasis.
		if(i >=9)
			upBounce = false;
		if(i == 0)
			upBounce = true;
		if(upBounce)
			i = (i+1)%10;
		else
			i = (i-1)%10;
		//draws the button images and their drop shadows which are created using round rect's with a low opacity
		g2d.setColor(new Color(0,0,0,100));
		g2d.fillRoundRect(xsize/2-116,ysize/2-194, 240, 64,2,2);
		g2d.drawImage(aboveButton, xsize/2-120,ysize/2-200, 240, 64,null);
		g2d.fillRoundRect(xsize/2-140,ysize/2-30-i, 300, 80,3,3);
		g2d.drawImage(currentButton, xsize/2-150,ysize/2-40-i, 300, 80,null);
		g2d.fillRoundRect(xsize/2-116,ysize/2+126, 240, 64,2,2);
		g2d.drawImage(belowButton, xsize/2-120,ysize/2+120, 240, 64,null);
		//draws the help string for CURRENTMENU
		g2d.setFont(new Font("Arial", Font.ITALIC, 30));
		FontMetrics fm = g2d.getFontMetrics();
		g2d.setColor(new Color(250,250,250, 120));
		g2d.fillRoundRect(0, ysize-80, xsize, 80, 2,2);
		g2d.setColor(Color.BLACK);
        //System.out.println(fm.stringWidth(CURRENTMENU.getText())+ " " + xsize/2);
		g2d.drawString(CURRENTOPTION.getHelpText(),xsize/2-(fm.stringWidth(CURRENTOPTION.getHelpText()))/2, ysize-10-(fm.getAscent() + (0 - (fm.getAscent() + fm.getDescent())) / 2));
	}
	private BufferedImage selectButtonImage(Options s){
		switch(s){
			case XRES: return xsizepic;
			case YRES: return ysizepic;
			case GOBACK: return gobackpic;
		}
		return null;
	}
	private void resetOptionPlaces(){
		CURRENTOPTION = Options.XRES; ABOVECURRENT = Options.GOBACK; BELOWCURRENT = Options.YRES;
		setImages();
		repaint();
	}
}
